﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.IO;

/*
 * 脚本功能：日志系统
 * 编写日期：2018.12.12
 * 编写人：HYZ
 */
namespace CFrame.Tools
{
    public enum LogColor
    {
        none,
        red,
        blue,
        green,
        yellow,
        gray,
        white,
    }

    public class Debuger
    {
        /// <summary>
        /// 是否开启日志输出
        /// </summary>
        public static bool UseDebug = true;
        /// <summary>
        /// 是否开启日志文件输出
        /// </summary>
        public static bool UseLogFile = false;

        /// <summary>
        /// 日志文件保存路径
        /// </summary>
        public static string FilePath
        {
            get
            {
                return filePath;
            }
            set
            {
                if (!Directory.Exists(value))
                {
                    Directory.CreateDirectory(value);
                }
                filePath = value;
            }
        }
        private static string filePath = Application.persistentDataPath+"/";

        public static string FileName
        {
            get { return fileName; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    fileName = value;
                }
            }
        }
        private static string fileName = string.IsNullOrEmpty(Application.productName) ? "Default" : Application.productName;


        /// <summary>
        /// 正常日志输出
        /// </summary>
        public static void Log(object message, LogColor color = LogColor.none)
        {
            if(UseLogFile)
                WriteLog(string.Format("【{0}】:{1}", "Log", message));

            if (UseDebug == false)
                return;

            if (color != LogColor.none)
            {
                string temp = "";
                switch (color)
                {
                    case LogColor.red:
                        temp = "red";
                        break;
                    case LogColor.blue:
                        temp = "blue";
                        break;
                    case LogColor.green:
                        temp = "green";
                        break;
                    case LogColor.yellow:
                        temp = "yellow";
                        break;
                    case LogColor.gray:
                        temp = "gray";
                        break;
                    case LogColor.white:
                        temp = "white";
                        break;
                    default:
                        temp = "white";
                        break;
                }
                Debug.LogFormat("<color={0}>【{1}】:{2}</color>",temp,FileName,message);
            }
            else
            {
                Debug.LogFormat("【{0}】:{1}",FileName,message);
            }
        }

        /// <summary>
        /// 警告日志
        /// </summary>
        public static void LogWarning(object message)
        {
            if(UseLogFile)
                WriteLog(string.Format("【{0}】:{1}", "Warning", message));

            if (UseDebug == false)
                return;

            Debug.LogWarningFormat("【{0}】:{1}", FileName, message);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        public static void LogError(object message)
        {
            if(UseLogFile)
                WriteLog(string.Format("【{0}】:{1}", "Error", message));

            if (UseDebug == false)
                return;

            Debug.LogErrorFormat("【{0}】:{1}", FileName, message);
        }


        /// <summary>
        /// 写入日志文件
        /// </summary>
        /// <param name="strList"></param>
        public static void WriteLog(params object[] strList)
        {
            if (strList.Count() == 0) return;
            string strDicPath = FilePath;
            string strPath = strDicPath + string.Format("{0:yyyy年-MM月-dd日}", DateTime.Now) + FileName +".txt";
            if (!Directory.Exists(strDicPath)) Directory.CreateDirectory(strDicPath);
            if (!File.Exists(strPath)) { using (FileStream fs = File.Create(strPath)) ; }
            string str = File.ReadAllText(strPath);
            StringBuilder sb = new StringBuilder();
            foreach (var item in strList)
            {
                sb.Append("\r\n" + DateTime.Now.ToString() + "-----" + item + "");
            }
            File.WriteAllText(strPath, sb.ToString() + "\r\n-----z-----\r\n" + str);
        }
    }
}
